package net.nampham.visioncontrol;

import net.nampham.visioncontrol.model.*;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;

public class VisionControl extends Activity {
    private GLSurfaceView mGLSurfaceView;

    /*
    public native String stringFromJNI();
	static {
		android.util.Log.d("visioncontrol", "before load lib");
        System.loadLibrary("test");
		android.util.Log.d("visioncontrol", "after load lib");
    }//*/
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.d("----------------------");
        Logger.d("program started");
        Logger.d(NativeTest.GetString());
        //Logger.d(NativeTest2.GetString());
        //Logger.d(stringFromJNI());
        Logger.d("reach here");
        
        CONFIG._MAIN_ACTIVITY = this;
        
        //load library
        
        //mGLSurfaceView = new MainRenderer2(this);
        mGLSurfaceView = new NativeRenderer(this);
        if (detectOpenGLES20()) {
            // Tell the surface view we want to create an OpenGL ES 2.0-compatible
            // context, and set an OpenGL ES 2.0-compatible renderer.
            mGLSurfaceView.setEGLContextClientVersion(2);
            mGLSurfaceView.setRenderer((GLSurfaceView.Renderer)mGLSurfaceView);
        } else {
            // Set an OpenGL ES 1.x-compatible renderer. In a real application
            // this renderer might approximate the same output as the 2.0 renderer.
        	//TODO: report error here and quit!
        	this.finish();
        }
        setContentView(mGLSurfaceView);
    }
    
    private boolean detectOpenGLES20() {
        ActivityManager am =
            (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo info = am.getDeviceConfigurationInfo();
        return (info.reqGlEsVersion >= 0x20000);
    }
    
    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
        mGLSurfaceView.onResume();
        Logger.d("program resumed");
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        mGLSurfaceView.onPause();
        Logger.d("program paused");
    }
    
    @Override
    protected void onDestroy(){
    	super.onDestroy();
    	Logger.d("program ended");
    }
}